﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace RGPWEB.admin.MCode
{
    public partial class ModifyMCode : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                TextBox1.Attributes.Add("onkeydown", "if(event.keyCode==13)  {document.all." + ButtonFind.ClientID + ".focus();document.all." + this.ButtonFind.ClientID + ".click();}");

            }
        }

        protected void ButtonFind_Click(object sender, EventArgs e)
        {
          //  DetailsView1.Visible = false;
          ////  DetailsView1.DataSourceID = null;


          //  if ((DropDownListClassGroup.Text !="") && (DropDownList2.Text == ""))
          //  {
          //      this.ButtonAdd_Click(sender, null);
          //      DetailsView1.Visible = false;
          //      return;
          //  }

          //  if ((DropDownListClassGroup.Text == "") && (DropDownList2.Text != ""))
          //  {
          //      this.ButtonAdd_Click(sender, null);
          //      DetailsView1.Visible = false;
          //      return;
          //  }
                  


            GridView1.DataSourceID = ObjectDataSource1.ID;
            TextBox1.Text = TextBox1.Text.Trim();

            //if (GridView1.Rows.Count > 1)
            //{

            //    Label lbItem = (GridView1.Rows[GridView1.Rows.Count - 2].FindControl("lbItem")) as Label;
            //    Label lbCode = (GridView1.Rows[GridView1.Rows.Count - 2].FindControl("lbCode")) as Label;
            //    Label lbType = (GridView1.Rows[GridView1.Rows.Count - 2].FindControl("lbType")) as Label;
            //    Label lbGroup = (GridView1.Rows[GridView1.Rows.Count - 2].FindControl("lbGroup")) as Label;
            //    Label lbGroupName = (GridView1.Rows[GridView1.Rows.Count - 2].FindControl("lbGroupName")) as Label;
            //    Label lbNote = (GridView1.Rows[GridView1.Rows.Count - 2].FindControl("lbNote")) as Label;

            //    TextBox tbfMItem = GridView1.FooterRow.FindControl("tbfMItem") as TextBox;
            //    TextBox tbfMCode = GridView1.FooterRow.FindControl("tbfMCode") as TextBox;
            //    TextBox tbfMType = GridView1.FooterRow.FindControl("tbfMType") as TextBox;
            //    TextBox tbfMGroup = GridView1.FooterRow.FindControl("tbfMGroup") as TextBox;
            //    TextBox tbfMGroupName = GridView1.FooterRow.FindControl("tbfMGroupName") as TextBox;
            //    TextBox tbfMNote = GridView1.FooterRow.FindControl("tbfMNote") as TextBox;

            //    tbfMItem.Text = lbItem.Text;
            //    tbfMCode.Text = lbCode.Text;
            //    tbfMType.Text = lbType.Text;
            //    tbfMGroup.Text = lbGroup.Text;
            //    tbfMGroupName.Text = lbGroupName.Text;
            //    tbfMNote.Text = lbNote.Text;
            //}


          
            // GridView1.DataBind();  不要加这个见下面解释

            /*
               GridView在绑定数据源方面同时支持DataSourceID与DataSource这两种属性，
             * 除了像DataGrid 1.0版原本的DataSource属性外，又增加另外一个DataSourceID属性，
             * 这两者之间有什么差别呢？以下是说明。
             * 
              DataSourceID：这个新属性是用于专门搭配ASP.NET 2.0的DataSource控件，
             * 如SqlDataSource、AccessDataSource等等，若GridView以DataSourceID指定DataSource控件
             * 就能够享有内置的分页、排序、更新、删除，一行程序都不必写，
             * 甚至连GridView.DataBind()都可以免了，因为它会自动做Binding操作，好处多多，
             * 故ASP.NET 2.0的数据绑定应该优先使用此属性。
             * 
              DataSource：这是以前就存在的属性，若GridView使用这个属性来绑定数据，
             * 那很抱歉不但要明确声明GridView.DataBind()，并且无法享受内置分页、排序、更新、删除的好处，请自己慢慢Coding吧！
             * 
               警告
             如果使用GridView通过DataSourceID绑定到数据源控件（如SqlDataSource等），
             * 您还是习惯性地下GridView.DataBind()指令进行数据绑定操作，
             * 结果会发生什么事情呢？这样会造成“重复绑定”，
             * 也就是您要多浪费一次系统绑定性能与内存，故不可不慎呀！
             */



        }

        protected void ButtonShowAll_Click(object sender, EventArgs e)
        {
            DetailsView1.Visible = false;
            DetailsView1.DataSourceID = null;
            GridView1.DataSourceID = ObjectDataSourceShowAll.ID;

        }

        protected void ButtonAdd_Click(object sender, EventArgs e)
        {
            DetailsView1.DataSourceID = ObjectDataSourceShowAll.ID;
            DetailsView1.Visible = true;
        }


        protected void ObjectDataSourceShowAll_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            if(ObjectDataSourceShowAll.SelectMethod ==  "GetDataByClassGroupName")
            e.InputParameters["classGroupName"] = DropDownListClassGroup.Text;

            if (ObjectDataSourceShowAll.SelectMethod == "GetDataByItem")
                e.InputParameters["classItem"] = DropDownList2.Text;


        }

        protected void DropDownListClassGroup_TextChanged(object sender, EventArgs e)
        {

             
          //  Response.Write("<script>alert('开始筛选!')</script>");
            ObjectDataSourceShowAll.SelectMethod = "GetDataByClassGroupName";
            GridView1.DataSourceID = ObjectDataSourceShowAll.ID;
            TextBox1.Text = DropDownListClassGroup.Text;
        }

        protected void DropDownList2_TextChanged(object sender, EventArgs e)
        {


            TextBox1.Text = DropDownList2.Text;
            TextBox1.DataBind();
            ObjectDataSourceShowAll.SelectMethod = "GetDataByItem";
            GridView1.DataSourceID = ObjectDataSourceShowAll.ID;
       
        }


        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            // Insert data if the CommandName == "Insert"

            // and the validation controls indicate valid data...

            if (e.CommandName == "Insert" && Page.IsValid)
            {

                // TODO: Insert new record...
              ObjectDataSourceInsert.Insert();

            }

           // string strTemp = null;
           // TextBox tbfMItem = this.GridView1.FooterRow.FindControl("tbfMItem") as TextBox;

           //// if (this.GridView1.Rows.Count > 10)
                
           //     strTemp = this.GridView1.Rows[2].Cells[2].Text.ToString();

           // tbfMItem.Text = strTemp;//(string)GridView1.Rows[GridView1.Rows.Count-3].Cells[5].Text.ToString();

           // Response.Write("---" + strTemp + "999");




        }

        protected void ButtonReAdd_Click(object sender, EventArgs e)
        {
            GridView1.DataSourceID = ObjectDataSourceInsert.ID;


        }

        protected void ObjectDataSourceInsert_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            // Programmatically reference Web controls in the inserting interface...

            TextBox tbfMItem = (TextBox)GridView1.FooterRow.FindControl("tbfMItem");
            TextBox tbfMCode = (TextBox)GridView1.FooterRow.FindControl("tbfMCode");
            TextBox tbfMType = (TextBox)GridView1.FooterRow.FindControl("tbfMType");
            TextBox tbfMGroup = (TextBox)GridView1.FooterRow.FindControl("tbfMGroup");
            TextBox tbfMGroupName = (TextBox)GridView1.FooterRow.FindControl("tbfMGroupName");
            TextBox tbfMNote = (TextBox)GridView1.FooterRow.FindControl("tbfMNote");
            
            // Set the ObjectDataSource's InsertParameters values...



            string textTemp = null;

            if (!string.IsNullOrEmpty(tbfMItem.Text)) textTemp = tbfMItem.Text;

            e.InputParameters["ClassItem"] = tbfMItem.Text;


            if (!string.IsNullOrEmpty(tbfMCode.Text)) textTemp = tbfMCode.Text;

            e.InputParameters["ClassCode"] = tbfMCode.Text;


            if (!string.IsNullOrEmpty(tbfMType.Text)) textTemp = tbfMType.Text;

            e.InputParameters["ClassType"] = tbfMType.Text;

            if (!string.IsNullOrEmpty(tbfMGroup.Text)) textTemp = tbfMGroup.Text;

            e.InputParameters["ClassGroup"] = tbfMGroup.Text;

            if (!string.IsNullOrEmpty(tbfMGroupName.Text)) textTemp = tbfMGroupName.Text;

            e.InputParameters["ClassGroupName"] = tbfMGroupName.Text;

            if (!string.IsNullOrEmpty(tbfMNote.Text)) textTemp = tbfMNote.Text;

            e.InputParameters["ClassNote"] = tbfMNote.Text;

          
            

          //  tbfMItem.Text = GridView1.Rows[GridView1.Rows.Count - 1].Cells[3].Text;


        //    Response.Write("<script>alert(tbfMItem.Text)</script>");
        
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
           
            string strTemp  =null;

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
               // if(GridView1.row
            }
            
            

            if (e.Row.RowType == DataControlRowType.Footer)
            {

                // Programmatically reference Web controls in the inserting interface...

               
             //    TextBox tbfMItem = e.Row.FindControl("tbfMItem") as TextBox;

            ///     if (this.GridView1.Rows.Count > 10) strTemp = this.GridView1.Rows[2].Cells[2].Text.ToString();

              ///   tbfMItem.Text = strTemp;//(string)GridView1.Rows[GridView1.Rows.Count-3].Cells[5].Text.ToString();

              ///   Response.Write("---" + strTemp + "999");
                
                // e.Row.Cells[2].Text = "1232345";

               // e.Row.FindControl("tdfMItem").
                //e.Row.Cells[2].Text = GridView1.Rows[GridView1.Rows.Count - 1].Cells[2].Text;
                //e.Row.Cells[3].Text = GridView1.Rows[GridView1.Rows.Count - 1].Cells[3].Text;
                //e.Row.Cells[4].Text = GridView1.Rows[GridView1.Rows.Count - 1].Cells[4].Text;
                //e.Row.Cells[5].Text = GridView1.Rows[GridView1.Rows.Count - 1].Cells[5].Text;
                //e.Row.Cells[6].Text = GridView1.Rows[GridView1.Rows.Count - 1].Cells[6].Text;
                //e.Row.Cells[7].Text = GridView1.Rows[GridView1.Rows.Count - 1].Cells[7].Text;


                 //e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Center;

                 //e.Row.Cells[1].ColumnSpan = 10;
                 //for (int i = 2; i <= 10; i++)
                 //{
                 //   e.Row.Cells[i].Visible = false;
                 //}
            }

          //  if (e.Row.RowType == DataControlRowType.DataRow)
         //   {
         //      TextBox tbfMItem = e.Row.FindControl("tbfMItem") as TextBox;

          //  }


        }

        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {

         //   TextBox txb = (TextBox)sender;//TextBox发送绑定请求   
         //   GridViewRow container = (GridViewRow)txb.NamingContainer;
         //   txb.Text = ((DataRowView)container.DataItem)[strColName].ToString();
          Label lbItem = (GridView1.Rows[e.NewSelectedIndex].FindControl("lbItem")) as Label;
          Label lbCode = (GridView1.Rows[e.NewSelectedIndex].FindControl("lbCode")) as Label;
          Label lbType = (GridView1.Rows[e.NewSelectedIndex].FindControl("lbType")) as Label;
          Label lbGroup = (GridView1.Rows[e.NewSelectedIndex].FindControl("lbGroup")) as Label;
          Label lbGroupName = (GridView1.Rows[e.NewSelectedIndex].FindControl("lbGroupName")) as Label;
          Label lbNote = (GridView1.Rows[e.NewSelectedIndex].FindControl("lbNote")) as Label;
  
          TextBox tbfMItem = GridView1.FooterRow.FindControl("tbfMItem") as TextBox;
          TextBox tbfMCode = GridView1.FooterRow.FindControl("tbfMCode") as TextBox;
          TextBox tbfMType = GridView1.FooterRow.FindControl("tbfMType") as TextBox;
          TextBox tbfMGroup = GridView1.FooterRow.FindControl("tbfMGroup") as TextBox;
          TextBox tbfMGroupName = GridView1.FooterRow.FindControl("tbfMGroupName") as TextBox;
          TextBox tbfMNote = GridView1.FooterRow.FindControl("tbfMNote") as TextBox;

          tbfMItem.Text = lbItem.Text;
          tbfMCode.Text = lbCode.Text;
          tbfMType.Text = lbType.Text;
          tbfMGroup.Text = lbGroup.Text;
          tbfMGroupName.Text = lbGroupName.Text;
          tbfMNote.Text = lbNote.Text;
          // GridViewRow container = (GridViewRow)tbfMItem.NamingContainer;

          // tbfMItem.Text = ((DataRowView)container.DataItem)["ClassItem"].ToString();

         //  DataRowView drv = (DataRowView)GridView1.;
           

           //   tbfMItem.Text =  GridView1.Rows[e.NewSelectedIndex].Cells[5].Text;

          

           // Response.Write("---" + L.Text+ "999");
        }


 

    }
}
